import {
	defineStore
} from "pinia"

let userInfo_key = "TardeAdminUserInfo"
let token_key = "tarde-token"

export default defineStore('userInfo', {
	state: () => ({
		loginStatus: false,
		userInfo: null,
		token: null
	}),
	getters: {
		LoginStatus: function(state) {
			if (!state.loginStatus || !state.token || !state.userInfo) {
				return false
			} else {
				return true
			}
		},
		GetUserInfo: function() {}
	},
	actions: {
		InitUserInfo: function() {
			let storeToken = this.token
			let storeInfo = this.userInfo
			if (!storeInfo) {
				let UserInfo = uni.getStorageSync(userInfo_key)
				if(UserInfo){
					this.userInfo = JSON.parse(UserInfo)
				}
			}
			if (!storeToken) {
				let userToken = uni.getStorageSync(token_key)
				this.token = userToken
			}
		},
		// 登录
		login: function(token, info) {
			this.token = token
			this.userInfo = info
			uni.setStorageSync("tarde-token", token)
			uni.setStorageSync("TardeAdminUserInfo", JSON.stringify(info))
			this.loginStatus = true
		},
		// 退出登录
		logout: function() {
			this.userInfo = null
			this.token = null
			this.loginStatus = false
			let userInfo_key = "TardeAdminUserInfo"
			let token_key = "tarde-token"
			// 清除本地token
			uni.removeStorageSync(token_key)
			// 清除本地用户信息
			uni.removeStorageSync(userInfo_key)
		}
	}
})